package com.wrench.elevator.tida.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.dao.JSGLDao;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.domain.wuye.Wuye_Power;
import com.wrench.elevator.tida.domain.wuye.Wuye_Role;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.util.WrenchUtil;

@Component
public class JSGLService
{
    private static final Logger logger = LoggerFactory.getLogger(JSGLService.class);
    
    @Resource
    private JSGLDao dao;
    
    
    public Wuye_Role getWuyeRoleByRoleId(String role_id) throws WrenchException{
    	if(WrenchUtil.hasNull(role_id)){
    		logger.error("==> param is not found , role_id = {} ",role_id);
    		throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
    	}
    	Wuye_Role role=null;
    	try {
			role=dao.getWuyeRoleByRoleId(role_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("==> role=dao.getWuyeRoleByRoleId(role_id) execute error , role_id = {} , error = {} ",role_id,e);
			throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
		}
    	
    	return role;
    }
    
    
    public List<Wuye_Power> getWuyePower()
        throws WrenchException
    {
        List<Wuye_Power> powers = null;
        try
        {
            powers = dao.getWuyePower();
        }
        catch (Exception e)
        {
            logger.error("==>  powers = dao.getWuyePower() execute error , error = {} ", e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        
        if (WrenchUtil.hasNull(powers))
        {
            logger.error("==>  powers = dao.getWuyePower() execute success but powers is null , powers = {} ", powers);
            throw new WrenchException(Result.GET_WUYE_POWER_FAIL_CODE, Result.GET_WUYE_POWER_FAIL_MESSAGE);
        }
        logger.debug("==>  powers = dao.getWuyePower() execute success , powers = {} ", powers);
        return powers;
        
    }
    
    public void addRole(String role_name, String[] power_id, ModelMap map)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(role_name, power_id))
        {
            logger.error("==> param is not found , role_name = {} , power_id = {} ", role_name, power_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
            
        }
        
        StringBuffer powerids = new StringBuffer();
        int len = power_id.length;
        for (int i = 0; i < len; i++)
        {
            powerids.append(power_id[i]);
            if (i != len - 1)
                powerids.append(",");
        }
        WrenchUtil.getParamMap(map, "role_name", role_name, "powerids", powerids.toString());
        try
        {
            dao.addRole(map);
        }
        catch (Exception e)
        {
            logger.error("==> dao.addRole(map) execute error , params = {} , error = {} ", JSON.toJSONString(map), e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        
        logger.debug("==> dao.addRole(map) execute  success , params = {} ", JSON.toJSONString(map));
        
    }
    
    public Page queryWuyeRoles(String current_page, String limit)
        throws WrenchException
    {
        
        if (WrenchUtil.hasNull(current_page, limit))
        {
            logger.error("==> param is not found , current_page = {} , limit = {}  ", current_page, limit);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.areInt(current_page, limit))
        {
            logger.error("==> string is not int type , current_page = {} , limit = {} ", current_page, limit);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        List<Wuye_Role> roles = null;
        try
        {
            roles =
                dao.queryWuyeRoles((Integer.valueOf(current_page) - 1) * Integer.valueOf(limit), Integer.valueOf(limit));
        }
        catch (Exception e)
        {
            logger.error("==> roles = dao.queryWuyeRoles((Integer.valueOf(current_page)-1)*Integer.valueOf(limit),Integer.valueOf(limit)) execute error , current_page = {} , limit = {} , error = {} ",
                current_page,
                limit,
                e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        
        if (WrenchUtil.hasNull(roles))
        {
            logger.error("==> roles = dao.queryWuyeRoles((Integer.valueOf(current_page)-1)*Integer.valueOf(limit),Integer.valueOf(limit)) execute success  but roles is null ,current_page = {} , limit = {} , roles = {}  ",
                current_page,
                limit,
                roles);
            throw new WrenchException(Result.QUERY_WUYE_ROLE_FAIL_CODE, Result.QUERY_WUYE_ROLE_FAIL_MESSAGE);
        }
        logger.debug("==> roles = dao.queryWuyeRoles((Integer.valueOf(current_page)-1)*Integer.valueOf(limit),Integer.valueOf(limit)) execute success  ,current_page = {} , limit = {} , roles = {}  ",
            current_page,
            limit,
            roles);
        
        int total = -1;
        try
        {
            total = dao.queryWuyeRolesCount();
        }
        catch (Exception e)
        {
            logger.error("==> total=dao.queryWuyeRolesCount() execute error , error = {} ", e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        if (total < 0)
        {
            logger.error("==> total=dao.queryWuyeRolesCount() execute success but total<0 , total = {}", total);
            throw new WrenchException(Result.QUERY_WUYE_ROLE_COUNT_FAIL_CODE, Result.QUERY_WUYE_ROLE_COUNT_FAIL_MESSAGE);
            
        }
        logger.error("==> total=dao.queryWuyeRolesCount() execute success , total = {}", total);
        Page page = new Page();
        page.setCurrent_page(Integer.valueOf(current_page));
        page.setLimit(Integer.valueOf(limit));
        page.setList(roles);
        page.setTotal(total);
        return page;
        
    }
    
    public void delRole(String role_id, ModelMap map)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(role_id))
        {
            logger.error("==> param is not found , role_id = {} ", role_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.isInt(role_id))
        {
            logger.error("==> string is not int type , role_id = {} ", role_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        WrenchUtil.getParamMap(map, "role_id", role_id);
        try
        {
            dao.delRole(map);
        }
        catch (Exception e)
        {
            logger.error("==> dao.delRole(map) execute error , role_id = {} , error = {}", role_id, e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.delRole(map) execute success , role_id = {} , map  = {}", role_id, JSON.toJSONString(map));
    }
    
    public void updateRole(String role_id, String role_name, String[] power_id, ModelMap map)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(role_id, role_name, power_id))
        {
            logger.error("==> param is not found , role_id = {} , role_name = {} , power_id = {} ",
                role_id,
                role_name,
                power_id);
            throw new WrenchException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        if (!WrenchUtil.isInt(role_id))
        {
            logger.error("==> string is not int type , role_id = {} ", role_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        StringBuffer powerids = new StringBuffer();
        int len = power_id.length;
        for (int i = 0; i < len; i++)
        {
            powerids.append(power_id[i]);
            if (i != len - 1)
                powerids.append(",");
        }
        WrenchUtil.getParamMap(map, "role_name", role_name, "powerids", powerids.toString(), "role_id", role_id);
        try
        {
            dao.updateRole(map);
        }
        catch (Exception e)
        {
            logger.error("==> dao.updateRole(map) execute error , map = {} , error = {}", JSON.toJSONString(map), e);
            throw new WrenchException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.updateRole(map) execute success , map = {} ", JSON.toJSONString(map));
    }
}
